<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑导航 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${subCategoryData.parentId}`">
          {{ subCategoryData.parentName }}
        </XtxBreadItem>
        <XtxBreadItem>{{ subCategoryData.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 筛选区 -->
      <div class="sub-filter">
        <!-- 品牌 -->
        <div class="item">
          <div class="head">品牌：</div>
          <div class="body">
            <a
              href="javascript:;"
              v-for="brand in subCategoryData.brands"
              :key="brand.id"
              @click="changeBrand(brand)"
              :class="{ active: brand.selected === true }"
              >{{ brand.name }}</a
            >
          </div>
        </div>
        <!-- 规格 -->
        <div
          class="item"
          v-for="item in subCategoryData.saleProperties"
          :key="item.id"
        >
          <div class="head">{{ item.name }}</div>
          <div class="body">
            <a
              href="javascript:;"
              v-for="i in item.properties"
              :key="i.id"
              @click="toggleSelected(i, item)"
              :class="{ active: i.selected === true }"
              >{{ i.name }}</a
            >
          </div>
        </div>
      </div>
      <div class="list-container">
        <!-- 排序区域 -->
        <div class="sub-sort">
          <div class="sort">
            <a
              href="javascript:;"
              @click="changeSort('price')"
              :class="{ active: sortField === 'price' }"
              >默认排序</a
            >
            <a
              href="javascript:;"
              @click="changeSort('publishTime')"
              :class="{ active: sortField === 'publishTime' }"
              >最新商品</a
            >
            <a
              href="javascript:;"
              @click="changeSort('orderNum')"
              :class="{ active: sortField === 'orderNum' }"
              >最高人气</a
            >
            <a
              href="javascript:;"
              @click="changeSort('evaluateNum')"
              :class="{ active: sortField === 'evaluateNum' }"
              >评论最多</a
            >
          </div>
        </div>

        <!-- 商品列表 -->
        <div class="sort-list-container">
          <GoodsItem v-for="good in goodList" :key="good.id" :goods="good" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, ref, unref } from 'vue'
import { useRoute } from 'vue-router'
// 导入 获取筛选数据API
import { findSubCategoryFilter, findSubCategoryGoods } from '@/api/category.js'
// 导入模板
import GoodsItem from '../components/goods-item.vue'
export default {
  name: 'SubCategory',
  components: {
    GoodsItem
  },
  setup () {
    const subCategoryData = ref({})
    const route = useRoute()
    async function getCategroyAllList () {
      const res = await findSubCategoryFilter(route.params.id)
      // console.log(route.params.id)
      subCategoryData.value = res.result
      initData(subCategoryData)

      function initData (data) {
        // 兼容ref或者非ref
        data = unref(data)
        // 品牌  subCategoryData.value.brands  没有‘全部’分类  我们自己添加一个全部的分类
        // 在品牌list中的头上添加一个全部分类  unshift
        data.brands.unshift({ name: '全部', id: null })
        // 添加一个专门的字段selected来控制 是否激活
        data.saleProperties.forEach(item => {
          item.properties.unshift({ name: '全部', id: null })
          // 给每一个规格添加一个专门用来控制是否激活的字段 selected true 激活 false 不激活
          item.properties.forEach(item => {
            item.selected = false
          })
        })
      }
    }
    onMounted(() => {
      getCategroyAllList()
    })
    // onBeforeRouteUpdate((to, from, next) => {
    //   getCategroyAllList(to.params.id)
    // })
    // 点击激活交互实现
    // 1.在现有的接口字段的基础上添加一个用来控制激活状态的字段
    // 2.使用排它思想 先把自己的同类全都设置为false 再把自己设置为true
    function toggleSelected (i, item) {
      // 排它 先把同类设置为false 再把自己设置为true
      item.properties.forEach(item => {
        item.selected = false
      })
      i.selected = true
    }

    // 排序区域功能
    const sortField = ref('price')
    // 按钮渲染类名,,加颜色
    function changeSort (type) {
      if (sortField.value === type) return
      sortField.value = type
      // 1.把传下来的sortType 赋值 reqData中的sortField字段
      reqParams.value.sortField = sortField
      // 2.修改完毕之后重新调用loadGoodList方法 获取最新的数据
      loadData()
    }
    // 渲染数据
    const goodList = ref([])
    // 请求参数
    const reqParams = ref({
      page: 1,
      pageSize: 20,
      categoryId: route.params.id,
      sortField: null, // 排序类别
      attrs: [], // 商品属性
      brandId: null // 品牌名称
    })
    async function loadData () {
      const res = await findSubCategoryGoods(reqParams.value)
      goodList.value = res.result.items
      console.log(res.result)
      console.log(reqParams.value.categoryId)
    }
    onMounted(() => {
      loadData()
    })
    // 更改品牌
    function changeBrand (brand) {
      reqParams.value.brandId = brand.id
      loadData()
    }

    return {
      subCategoryData,
      toggleSelected,
      sortField,
      changeSort,
      goodList,
      changeBrand,
      reqParams
    }
  }
}
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
}
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    padding: 10px 0;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
.list-container {
  margin-top: 20px;
  padding: 0 20px;
  background: #fff;
}
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
.sort-list-container {
  display: flex;
  flex-wrap: wrap;
  .goods-item {
    flex: 1;
  }
}
</style>
